/**
 * 股票相关工具函数
 */

/**
 * 处理金额显示
 * @param {number} data 金额数据
 * @returns {string} 格式化后的金额字符串
 */
export function dealMoney(data) {
  let end_name = data;
  if (data < 1000) {
    end_name = (data / 100).toFixed(1) + "百";
  } else if (data < 10000) {
    end_name = (data / 1000).toFixed(1) + "千";
  } else if (data < 100000) {
    end_name = (data / 10000).toFixed(1) + "万";
  }
  return end_name;
}

/**
 * 处理状态切换
 * @param {number} num 状态码
 * @returns {string} 状态描述
 */
export function switchAction(num) {
  switch (num) {
    case 0: return "待定";
    case 1: return "横盘中0.5到-1";
    case 2: return "盈利";
    case 3: return "亏损";
    default: return "";
  }
}

/**
 * 处理月度数据
 * @param {Object} dataList 数据列表
 * @returns {Array} 处理后的月度数据
 */
export function dealMoth(dataList) {
  let item1 = [];
  let time1 = null;
  let time2 = [];
  let array1 = [];

  dataList.time.forEach((element, index) => {
    if (time1 == element.slice(0, 4)) {
      item1.push(dataList.array[index]);
      time2.push(element.slice(0, 7));
      if (index + 1 == dataList.time.length) {
        array1.push({
          name: time1,
          array: dealTime(item1, time2),
          time: time2,
        });
      }
    } else {
      if (time1 !== null) {
        array1.push({
          name: time1,
          array: dealTime(item1, time2),
          time: time2,
        });
      }

      item1 = [];
      time1 = null;
      time1 = element.slice(0, 4);
      time2 = [];
      time2.push(element.slice(0, 7));
      item1.push(dataList.array[index]);
    }
  });

  return array1;
}

/**
 * 处理时间数据
 * @param {Array} data 数据数组
 * @param {Array} time 时间数组
 * @returns {Array} 处理后的时间数据
 */
export function dealTime(data, time) {
  let data1 = [];
  let time1 = [];
  var index1 = 0;
  for (let i = 1; i < 13; i++) {
    let num1 = i < 10 ? "-0" + i : "-" + i;
    let time_item = time[index1] == undefined ? "" : time[index1].slice(4, 7) + "";

    if (time_item == num1) {
      data1.push(data[index1]);
      index1 = index1 + 1;
    } else {
      data1.push("--");
    }
  }
  return data1;
}

/**
 * 获取Ajax数据
 * @param {string} url 请求地址
 * @returns {Object} 响应数据
 */
export function getAjax2(url) {
  if (url == "") {
    return "";
  } else {
    var htmlobj = $.ajax({
      url: url,
      async: false,
    });
    return htmlobj.responseJSON.data;
  }
} 